<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


include_once(APPPATH.'controllers/objectController.php');

class Users extends ObjectController {
	var $workgroup = 'desktop_ctl_system';
	var $appname = 'desktop';
	var $modelName = 'desktop/admin_user_model';
	var $detail_views = array(
		'detail_basic'=>array('func'=>'_detail_basic','label'=>'用户信息', 'default'=>TRUE)
	);
	var $default_filter = array('disabled'=>"false");

	var $search_options = array('username'=>'用户名','realname'=>'姓名');

	function __construct() {
		parent::__construct();
	}

	function index(){
		$finder = array(
			'name'=>'user_view',
			'title'=>'用户管理',
			'buildindelete'=>true,
			'buildinrecycle'=>true,
			'actions'=>array(
				array('label'=>'新建用户', 'href'=>'/desktop/users/add')
			),
			'row_ops'=>array(
				'edit'=>array('title'=>'编辑用户' , 'label'=>'编辑')
			),
			'detailitem'=>false
		);
		$this->render_finder($finder);
	}
	
	function modifier_status($colName, &$rows, &$colDef){
		$status = array('true'=>'<span style="color:green;">启用</span>', 'false'=>'<span style="color:red;">禁用</span>');
		foreach($rows as &$row){
				$row['status'] = $status[$row['status']]; 
		}
		unset($row);
	}
	
	function modifier_super($colName, &$rows, &$colDef){
		$super = array('true'=>'<span style="color:blue;">是</span>', 'false'=>'否');
		foreach($rows as &$row){
				$row['super'] = $super[$row['super']]; 
		}
		unset($row);		
	}
	
	function showChgPw(){
		$userId = $this->input->get_post('userId');
		$data['userId'] = $userId;
		$this->load->view('desktop/user/changePasswod' , $data);
	}
	
	function changePassword(){
		$userId = $this->input->get_post('userId');
		$password = $this->input->get_post('password');
		$newPassword = $this->input->get_post('newPassword');
		$rePassword = $this->input->get_post('rePassword');
		if(strlen($newPassword) < 6 || strlen($newPassword) > 16){
			$this->out_put_jcmd(false,'密码由6-16个字符组成，区分大小写');
			return;
		}
		if($newPassword != $rePassword){
			$this->out_put_jcmd(false,'两次输入密码不一致');
			return;
		}
		//验证密码
		$this->load->model('desktop/admin_user_model' , 'model');
		$user = $this->model->findByPrimaryKey($userId);
		if($user['password'] != md5($password)){
			$this->out_put_jcmd(false,'密码错误，请重新输入');
			return;
		}
		//修改密码
		$this->model->changePassword($userId , $newPassword);
		$this->out_put_jcmd(true , '密码修改成功');
	}
	function showResetPwd(){
		$userId = $this->input->get_post('userId');
		$data['userId'] = $userId;
		$this->load->view('desktop/user/resetPasswod' , $data);
	}
	function resetPassword(){
		$userId = $this->input->get_post('userId');
		$newPassword = $this->input->get_post('newPassword');
		$rePassword = $this->input->get_post('rePassword');
		if(strlen($newPassword) < 6 || strlen($newPassword) > 16){
			$this->out_put_jcmd(false,'密码由6-16个字符组成，区分大小写');
			return;
		}
		if($newPassword != $rePassword){
			$this->out_put_jcmd(false,'两次输入密码不一致');
			return;
		}
		//验证密码
		$this->load->model('desktop/admin_user_model' , 'model');

		//修改密码
		$this->model->changePassword($userId , $newPassword);
		$this->out_put_jcmd(true , '密码修改成功');
	}
	function edit(){
		//查询用户
		$userId = $this->input->get_post('id');
		$this->load->model('desktop/admin_user_model' , 'model');
		$user = $this->model->findByPrimaryKey($userId);
		//查询所有用户组
		$this->load->model('desktop/admin_usergroup_model' , 'ugmodel');
		$aGroups = $this->ugmodel->findAllUsergroups();
		$data = array('user'=>$user , 'agroups'=>$aGroups);
		//查询用户所在用户组
		$groups = $this->ugmodel->findUserGroups($userId);
		$groupIds = array();
		foreach($groups as $group){
			$groupIds[] = $group['groupId'];
		}
		$user['groupIds'] = $groupIds;
		//显示页面
		$data = array('user'=>$user , 'aGroups'=>$aGroups);
		$this->page('desktop_ctl_system' , 'desktop/user/edituser' , $data);
	}

	function add(){
		//查询用户组
		$this->load->model('desktop/admin_usergroup_model' , 'ugmodel');
		$agroups = $this->ugmodel->findAllUsergroups();
		$data = array('agroups'=>$agroups);
		//显示页面
		$this->page('desktop_ctl_system' , 'desktop/user/adduser' , $data);
	}

	function editUser(){
		$user = $this->input->get_post('user');
		$groupIds = $this->input->get_post('groupIds');
		$this->load->model('desktop/admin_user_model','model');
		$user['status'] = $user['status'] ? 'true' : 'false';
		$this->model->editUser($user , $groupIds);	
		$this->out_put_jcmd(true , '操作成功' , '/desktop/users/index');
	}
	
	function addUser(){
		$user = $this->input->get_post('user');
		$groupIds = $this->input->get_post('groupIds');
		$this->load->model('desktop/admin_user_model','model');
		if(!$user['username']){
			$this->out_put_jcmd(false,'请输入用户名');
			return;
		}
		//判断是否有重名用户
		$tUser = $this->model->findUserByName($user['username']);
		if(!empty($tUser)){
			$this->out_put_jcmd(false,'用户名'.$user['username'].'已经被使用');
			return;
		}
		
		if(!$user['realname']){
			$this->out_put_jcmd(false,'请输入姓名');
			return;
		}
		if(!$user['mobile']){
			$this->out_put_jcmd(false,'请输入手机号码');
			return;
		}
		if(!$user['password'] || !$user['rePassword']){
			$this->out_put_jcmd(false,'请输入密码');
			return;
		}
		if(strlen($user['password']) < 6 || strlen($user['password']) > 16){
			$this->out_put_jcmd(false,'密码由6-16个字符组成，区分大小写');
			return;
		}
		if($user['password'] != $user['rePassword']){
			$this->out_put_jcmd(false,'两次输入密码不一致');
			return;
		}
		
		unset($user['rePassword']);
		$user['status'] = $user['status'] ? 'true' : 'false';
		$user['super'] = 'false';
		$user['lastlogin'] = date("Y-m-d H:i:s");
		$user['super'] = 'false';
		$this->model->addUser($user , $groupIds);	
		$this->out_put_jcmd(true , '操作成功' , '/desktop/users/index');
	}

	function _detail_basic(){
		//查询用户组
		$userid = $this->input->get_post('id');
		$this->load->model('desktop/admin_user_model' , 'model');
		$user = $this->model->findByPrimaryKey($userid);
		$data = array('user'=>$user);
		$this->load->view('desktop/user/detail_base' , $data);
	}
}
